//
//  SearchReplyViewController.m
//  pushNewsManager
//
//  Created by Jobs on 8/5/17.
//  Copyright © 2017 com.Iori. All rights reserved.
//

#import "SearchReplyViewController.h"
#import "Header.h"
#import "SearchCMTApi.h"
#import "CMTEntity.h"
#import "SearchReplyResultViewController.h"

@interface SearchReplyViewController ()
@property(strong ,nonatomic) NSArray<CMTEntity*> *dataSource;
@end

@implementation SearchReplyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSDateFormatter *formater = [NSDateFormatter dateFormatterWithFormat:@"yyyy-MM-dd HH:mm"];
    NSString *title = [formater stringFromDate:[NSDate date]];
    NSString *titleLastDate = [formater stringFromDate:[formater dateFromString:@"2017-07-07 00:00"]];
    [self.btnStartDate setTitle:titleLastDate forState:UIControlStateNormal];
    [self.btnEndDate setTitle:title forState:UIControlStateNormal];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    SearchReplyResultViewController *vc = segue.destinationViewController;
    vc.dataSource = self.dataSource;
}


-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
}

- (IBAction)btnDate_click:(UIButton *)sender
{
    WS(weakSelf);
    IoriDatePicker *picker = [[IoriDatePicker alloc]
                              initWithFrame:CGRectMake(0,
                                                       0,
                                                       weakSelf.view.frame.size.width,
                                                       216)];
    picker.datePickerMode = UIDatePickerModeDate;
    [picker showDatePickerInViewController:self okCallbackHandler:^(UIDatePicker *datePicker) {
        NSDateFormatter *formater = [NSDateFormatter dateFormatterWithFormat:@"yyyy-MM-dd HH:mm"];
        NSString *title = [formater stringFromDate:datePicker.date];
        [sender setTitle:title forState:UIControlStateNormal];
        
    } cancelCallbackHandler:^{
        ;
    }];
}

- (IBAction)btnSearch_click:(UIButton *)sender
{
    SearchCMTApi *api = [SearchCMTApi new];
    api.admin_id = [UserInfo sharedUser].admin.admin_id;
    if(self.radType.selectedButton.tag == 0)
    {
        api.keyword = self.txtSearch.text;
    }
    else if(self.radType.selectedButton.tag == 1)
    {
        api.nickname = self.txtSearch.text;
    }
    else if (self.radType.selectedButton.tag == 2)
    {
        api.member_id = self.txtSearch.text;
    }
    api.stime = [self.btnStartDate titleForState:UIControlStateNormal];
    api.etime = [self.btnEndDate titleForState:UIControlStateNormal];
    api.limit = @"1000";
    WS(weakSelf);
    [MessageBox displayLoadingInView:weakSelf.view];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        [MessageBox removeLoading];
        if([IoriJsonHelper getErrorCode:request] == 100)
        {
            [weakSelf.view endEditing:YES];
            weakSelf.dataSource = [NSArray yy_modelArrayWithClass:[CMTEntity class] json:[IoriJsonHelper getDictForKey:@"list" fromDict:request.responseJSONObject]];
            [weakSelf performSegueWithIdentifier:@"segueResult" sender:sender];
        }
        else
        {
            [weakSelf showMessage:[IoriJsonHelper getError:request]];
        }
    } failure:^(YTKBaseRequest *request) {
        [weakSelf showMessage:[request responseString]];
        [MessageBox removeLoading];
    }];
    
    
}
@end
